package leetcode.code0804;

import leetcode.IDebug;


public class Solution1 implements IDebug {

	String[] morse = new String[] { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-",
			".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--",
			"--.." };

	public int uniqueMorseRepresentations(String[] words) {
		root = new Node();
		int cnt = 0;
		for (String word : words) {
			char[] cs = word.toCharArray();
			Node node = root;
			for (char element : cs) {
				for (char c : morse[element - 'a'].toCharArray()) {
					node = node.insert(c);
				}
			}
			if (!node.isEnd()) {
				cnt++;
			}
			node.end();
		}
		return cnt;
	}

	private Node root;

	private class Node {
		Node dot;
		Node line;

		static final char D = '.';
		static final char L = '-';

		public Node() {

		}

		boolean end;

		public Node insert(char cur) {
			Node ans = null;
			if (cur == D) {
				if (this.dot == null) {
					this.dot = new Node();
				}
				ans = this.dot;
			} else {
				if (this.line == null) {
					this.line = new Node();
				}
				ans = this.line;
			}
			return ans;
		}

		public void end() {
			this.end = true;
		}

		public boolean isEnd() {
			return this.end;
		}

	}



	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
